package top.xchch.workdemo.func04;

import java.util.Random;
import java.util.concurrent.*;

/**
 * 线程同步方式四: CountDownLatch控制线程间协作, 同时实现main线程等待
 *
 * 背景. 10个人过桥, 每次只允许3个人过桥, 每个人过桥时间为500-600毫秒, 计算总的过桥时间
 *
 * @author sai.zhang
 * @date 2021-10-17 19:29
 */
public class Main {
    private static ExecutorService es = Executors.newFixedThreadPool(4);

    private static CountDownLatch cdl = new CountDownLatch(10);
    private static Semaphore semaphore = new Semaphore(3);

    private static final int PERSON_NUM = 10;

    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();

        for (int i = 0; i < PERSON_NUM; i++) {
            Person p = new Person("p-" + i);
            es.submit(() -> {
                try {
                    semaphore.acquire();
                } catch (InterruptedException e) {
                    System.out.println("出错了, " + e.getMessage());
                }

                try {
                    String crossResult = p.crossBridge(new Random().nextInt(100) + 500);
                    System.out.println(crossResult);
                } finally {
                    semaphore.release();
                    cdl.countDown();
                }
            });
        }

        cdl.await();
        System.out.println("使用时间: "+ (System.currentTimeMillis() - start) + " ms");
        es.shutdown();
    }
}

class Person {

    public String name;

    public Person(String name) {
        this.name = name;
    }

    public String crossBridge(int usedTime) {
        try {
            Thread.sleep(usedTime);
        } catch (InterruptedException e) {
            System.out.println("出错了, " + e.getMessage());
        }
        return this.name + "过河完成, 用时 " +  usedTime + " ms";

    }

}